﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Web.UI;
using System.Web.UI.HtmlControls;
using MxWeiXinPF.Common;
using System.Data;
using System.Text;
namespace MxWeiXinPF.Web.shopmgr.product
{
    public partial class productSetSaleSelect : ManagePage
    {
        protected MxWeiXinPF.Model.manager admin_info;
        private MxWeiXinPF.BLL.wx_shop_product caBll = new MxWeiXinPF.BLL.wx_shop_product();
        protected int catalogId = 0;
        protected int category_id;
        protected string keywords = string.Empty;
        protected int page;
        protected int pageSize;
        protected int shopid = 0;
        protected int totalCount;

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("productSetSaleSelect.aspx", "category_id={0}&keywords={1}", new string[] { this.category_id.ToString(), this.txtKeywords.Text }));
        }

        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder builder = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                builder.Append(" and  (isnull(productname,'')  like '%" + _keywords + "%' or isnull(shortdesc,'')  like '%" + _keywords + "%' or isnull(sku,'')  like '%" + _keywords + "%'  or isnull(zhunzi,'')  like '%" + _keywords + "%' or isnull(factory,'')  like '%" + _keywords + "%' or isnull(wareid,'')  like '%" + _keywords + "%') ");
            }
            builder.Append(" and  guadanstatus='1'");
            return builder.ToString();
        }

        protected void ddlCategoryId_SelectedIndexChanged(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("productSetSaleSelect.aspx", "category_id={0}&keywords={1}", new string[] { this.ddlCategoryId.SelectedValue, this.keywords }));
        }

        private int GetPageSize(int _default_size)
        {
            int num;
            if (int.TryParse(Utils.GetCookie("productSetSaleSelect_list_page_size"), out num) && (num > 0))
            {
                return num;
            }
            return _default_size;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.keywords = MXRequest.GetQueryString("keywords");
                this.category_id = MXRequest.GetQueryInt("category_id");
                this.pageSize = this.GetPageSize(10);
                if (!this.Page.IsPostBack)
                {
                    this.admin_info = base.GetAdminInfo();
                    this.TreeBind();
                    this.RptBind(this.category_id, this.CombSqlTxt(this.keywords), "sort_id asc,id desc");
                }
            }
            catch (Exception ex)
            {
                
            }

        }

        private void RptBind(int _category_id, string _strWhere, string _orderby)
        {
            if (this.category_id > 0)
            {
                this.ddlCategoryId.SelectedValue = _category_id.ToString();
            }
            _strWhere = " upselling=1   " + _strWhere;
            this.lblWid.Text = this.admin_info.id.ToString();
            this.page = MXRequest.GetQueryInt("page", 1);
            this.txtKeywords.Text = this.keywords;
            DataSet set = this.caBll.GetList(this.admin_info.id, this.category_id, this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataSource = set;
            this.rptList.DataBind();
            this.txtPageNum.Text = this.pageSize.ToString();
            string linkUrl = Utils.CombUrlTxt("productSetSaleSelect.aspx", "category_id={0}&keywords={1}&page={2}", new string[] { _category_id.ToString(), this.keywords, "__id__" });
            this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, linkUrl, 8);
        }

        private void TreeBind()
        {
            DataTable wCodeList = new MxWeiXinPF.BLL.wx_shop_category().GetWCodeList(39, 0);
            this.ddlCategoryId.Items.Clear();
            this.ddlCategoryId.Items.Add(new ListItem("所有类别", ""));
            foreach (DataRow row in wCodeList.Rows)
            {
                string str = row["id"].ToString();
                int num = int.Parse(row["class_layer"].ToString());
                string text = row["title"].ToString().Trim();
                if (num == 1)
                {
                    this.ddlCategoryId.Items.Add(new ListItem(text, str));
                }
                else
                {
                    text = "├ " + text;
                    text = Utils.StringOfChar(num - 1, "　") + text;
                    this.ddlCategoryId.Items.Add(new ListItem(text, str));
                }
            }
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int num;
            if (int.TryParse(this.txtPageNum.Text.Trim(), out num) && (num > 0))
            {
                Utils.WriteCookie("productSetSaleSelect_page_size", num.ToString(), 0x3840);
            }
            base.Response.Redirect(Utils.CombUrlTxt("productSetSaleSelect.aspx", "category_id={0}&keywords={1}", new string[] { this.category_id.ToString(), this.keywords }));
        }
    }
}